package com.ysx.oa.holiday.controller;

import com.ysx.oa.holiday.bean.HolidayBean;
import com.ysx.oa.holiday.service.HolidayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 王伟雄
 * @date 2021-11-22 15:54:59
 * @Version 1.0
 */
@Controller
@RequestMapping("/holiday")
public class HolidayController {
    @Autowired
    private HolidayService service;


    @RequestMapping("/teacher")
    public String index() {
        return "TeacherT";
    }


    // http://192.168.1.233:8080/kaoqin/holiday/addByStudent.do
    @RequestMapping(value = "/addByStudent.do",method = RequestMethod.POST)
    @CrossOrigin
    public Map<String,Object> addByStudent(@RequestBody HolidayBean bean,HttpServletRequest request){
        Map<String,Object> map = new HashMap();
        HttpSession session = request.getSession();
        Object userId = session.getAttribute("userId");
        Integer id=(Integer) userId;
        bean.setStudentId(id);
        Integer rs = service.addByStudent(bean);
        if(rs==0){
            map.put("code",500);
            map.put("message","failure");
        }else {
            map.put("code",200);
            map.put("message","success");
        }
        return map;
    }

    // http://localhost:8080/kaoqin/holiday/findByStudent.do
    @RequestMapping(value = "/findByStudent.do",method = RequestMethod.GET)
    @CrossOrigin
    public Map<String,Object> findByStudent(HttpServletRequest request){
        HttpSession session = request.getSession();
        Object userId = session.getAttribute("userId");
        Integer id=(Integer) userId;
        Map<String,Object> map = new HashMap();
        List<HolidayBean> byStudent = service.findByStudent(id);

        map.put("code",200);
        map.put("message","success");
        map.put("data",byStudent);
        return map;
    }

    // http://localhost:8080/kaoqin/holiday/deleteByStudent.do
    @RequestMapping(value = "/deleteByStudent.do",method = RequestMethod.POST)
    @CrossOrigin
    public Map<String,Object> deleteByStudent(@RequestBody HolidayBean holidayBean){
        Map<String,Object> map = new HashMap();
        Integer rs = service.deleteByStudent(holidayBean);

        if(rs==0){
            map.put("code",500);
            map.put("message","failure");
        }else {
            map.put("code",200);
            map.put("message","success");
        }
        return map;
    }

    // http://localhost:8080/kaoqin/holiday/updateByTeacher.do
    @RequestMapping(value = "/updateByTeacher.do",method = RequestMethod.POST)
    @CrossOrigin
    public Map<String,Object> updateByTeacher(@RequestBody HolidayBean holidayBean){
        Map<String,Object> map = new HashMap();
        Integer rs = service.updateByTeacher(holidayBean);

        if(rs==0){
            map.put("code",500);
            map.put("message","failure");
        }else {
            map.put("code",200);
            map.put("message","success");
        }
        return map;
    }

    // http://localhost:8080/kaoqin/holiday/findByTeacher.do
    @RequestMapping(value = "/findByTeacher.do",method = RequestMethod.GET)
    @CrossOrigin
    public Map<String,Object> findByTeacher(HttpServletRequest request){

        HttpSession session = request.getSession();
        System.out.println(session.getId());
        Object userId = session.getAttribute("userId");
        Integer id=(Integer) userId;
        Map<String,Object> map = new HashMap();
        Integer flag=900100;
        List<HolidayBean> byStudent = service.findByTeacher(id,flag);

        map.put("code",200);
        map.put("message","success");
        map.put("data",byStudent);
        return map;
    }

    // http://localhost:8080/kaoqin/holiday/findByTeacherApprove.do
    @RequestMapping(value = "/findByTeacherApprove.do",method = RequestMethod.GET)
    @CrossOrigin
    public Map<String,Object> findByTeacherApprove(HttpServletRequest request){
        HttpSession session = request.getSession();
        Object userId = session.getAttribute("userId");
        Integer id=(Integer) userId;
        Map<String,Object> map = new HashMap();
        Integer flag=900200;
        List<HolidayBean> byStudent = service.findByTeacher(id,flag);

        map.put("code",200);
        map.put("message","success");
        map.put("data",byStudent);
        return map;
    }
}
